
local tonumber = tonumber
local tostring = tostring

local log = ngx.log

--local error = error


local _M = { _VERSION = '0.09' }
local mt = { __index = _M }

_M._VERSION = '0.01'

local key = 'ok'

local rule_map = {
 ok     = "OK",
 c40001 = "参数是一个必选参数，当前值为null",   --索引为字符串，key = "web",
 c40002 = "参数必须是一个数字，当前值 %s 不是一个数字"
 }


-- 必须存在
function _M.__chk_require(self, arg_value, rule_arg)

	if arg_value == '' or arg_value == nil then
		key = 'c40001'
	end
	return _M.voidInfo(key,rule_map[key])
end

-- 检查输入是否是一个数字,并且在某个范围
function _M.__chk_num(self, arg_value, ruleArg)

	log(ngx.DEBUG, "argValue==" .. tostring(arg_value)  .. "ruleArg==" , ruleArg )

    local tmp_val = tonumber(arg_value)

	if not tmp_val then
		key = 'c40002'
	end

	return _M.voidInfo(key,string.format(rule_map[key], tostring(arg_value)) )
end

-- 检查值是否在某个范围
function _M.__chk_limit(self, arg_value, rule_arg)
    
    local tmp_val = tonumber(arg_value)

	if not tmp_val then
		key = 'c40002'
	end
	
	return _M.voidInfo(key,string.format(rule_map[key], tostring(arg_value)) )
end


function _M.voidInfo(code, msg)

	return {code = code, msg=msg}
end
   
return _M